import Vue from "vue";
import VueI18n from "vue-i18n";
import elEn from "element-ui/lib/locale/lang/en";
import elZh from "element-ui/lib/locale/lang/zh-CN";
import zh from "./zh";
import en from "./en";
import store from "@/store";

Vue.use(VueI18n);

const messages = {
  "zh-CN": {
    ...zh,
    ...elZh,
  },
  en: {
    ...en,
    ...elEn,
  },
};

export const defaultLang = "zh-CN";

if (!store.state.settings.defaultLang) {
  const lang = navigator.language || navigator.browserLanguage;
  Object.keys(messages).map((key) => {
    lang.includes(key) && store.commit("settings/setDefaultLang", key);
  });
}

const i18n = new VueI18n({
  silentTranslationWarn: true,
  silentFallbackWarn: true,
  locale: "zh-CN",
  fallbackLocale: defaultLang,
  messages,
});

export default i18n;
